#!/bin/bash

systemsetting="recalbox_settings"
config_script=/recalbox/scripts/recalbox-config.sh

rb_volume_configure() {
    settingsVolume="`$systemsetting -command load -key audio.volume`"
    if [ "$settingsVolume" != "" ];then
	recallog "setting audio volume to $settingsVolume"
        eval $config_script "volume" "$settingsVolume" 2>&1 | recallog
    fi
}

rb_audio_save() {
    recallog "Saving ALSA mixers configuration"
    alsactl -f /recalbox/share/system/configs/asound.state store
}

rb_audio_configure() {
    recallog "Restoring ALSA mixers configuration"
    alsactl -f /recalbox/share/system/configs/asound.state restore
    settingsAudio="`$systemsetting -command load -key audio.device`"
    if [[ "$settingsAudio" == "" ]];then
        settingsAudio="auto"
    fi
    recallog "setting audio to $settingsAudio"
    eval $config_script "audio" "$settingsAudio" 2>&1 | recallog
}

rb_gpio_configure() {
    settings_gpio_mk="`$systemsetting -command load -key controllers.gpio.enabled`"
    if [ "$settings_gpio_mk" == "1" ];then
	settings_gpio_map="`$systemsetting -command load -key controllers.gpio.args`"
	[ "$settings_gpio_map" == "" ] && settings_gpio_map="map=1,2"
	recallog "enabling mk_arcade_joystick_rpi"
        eval $config_script "module" "load" mk_arcade_joystick_rpi "$settings_gpio_map" 2>&1 | recallog
    else
	# mk disabled let's check for db9
	settings_db9="`$systemsetting -command load -key controllers.db9.enabled`"
	if [ "$settings_db9" == "1" ];then
            settings_db9_map="`$systemsetting -command load -key controllers.db9.args`"
            recallog "enabling db9"
            eval $config_script "module" "load" db9_gpio_rpi "$settings_db9_map" 2>&1 | recallog
	else 
	    # db9 and mk disabled let's check for gamecon
            settings_gamecon="`$systemsetting -command load -key controllers.gamecon.enabled`"
            if [ "$settings_gamecon" == "1" ];then
                settings_gamecon_map="`$systemsetting -command load -key controllers.gamecon.args`"
                recallog "enabling gamecon"
                eval $config_script "module" "load" gamecon_gpio_rpi "$settings_gamecon_map" 2>&1 | recallog
	    fi
	fi
    fi
}

rb_keyboad_lang() {
    settings_lang="`$systemsetting -command load -key system.language`"
    settings_kb="`$systemsetting -command load -key system.kblayout`"
    if [[ "$settings_kb" != "" ]];then
        recallog "setting keyboard layout to ${settings_kb}"
	loadkeys "$settings_kb" 2>&1 | recallog
    else
        keyboardmap=`echo "$settings_lang" | cut -c1-2`
	if [[ "$keyboardmap" != "" ]]; then
            recallog "setting keyboard layout to ${keyboardmap}"
            loadkeys "$keyboardmap" 2>&1 | recallog
	fi
    fi
}

rb_timezone() {
    settings_timezone="`$systemsetting -command load -key system.timezone`"
    [[ -z $settings_timezone ]] && settings_timezone="Europe/Paris"
    if [[ "$settings_timezone" != "" && -f "/usr/share/zoneinfo/$settings_timezone" ]];then
        recallog "setting timezone to $settings_timezone"
        echo "$settings_timezone" > /var/timezone
        ln -sf "/usr/share/zoneinfo/$settings_timezone" /var/localtime
    fi
}

rb_hostname() {
    settings_hostname="`$systemsetting -command load -key system.hostname`"
    if [[ "$settings_hostname" != "" ]];then
        recallog "setting hostname to $settings_hostname"
        hostname "$settings_hostname"
    fi
}

rb_xarcade2jstick() {
    settings_xarcade="`$systemsetting -command load -key controllers.xarcade.enabled`"
    if [[ "$settings_xarcade" == "1" ]];then
        recallog "starting xarcade2jstick"
        # First try : suppose a real X-Arcade is plugged
        /usr/bin/xarcade2jstick -d
        if [ $? -ne "0" ]
        then
            # the user must have a keyboard encoder then
            recallog "xarcade2jstick : no official stick found, looking for keyboard encoder now ..."
            for dev in /recalbox/share_init/system/configs/xarcade2jstick/*
            do
                dev=$(basename "$dev")
                [ -h "/dev/input/by-id/$dev" ] && recallog "xarcade2jstick : Found $dev ! Starting xarcade2jstick ..." && /usr/bin/xarcade2jstick -d -e "/dev/input/by-id/$dev" && break
            done
        fi
    fi
}

case "$1" in
  start)
	recallog "starting new log"

	# Dos carriage return characters
	recallog "converting dos to unix carriage return characters"
	sed -i 's/\r//g' /recalbox/share/system/recalbox.conf
	
	# configure
	rb_gpio_configure&    # 0.9 start by the gpio while it's the longer
	rb_keyboad_lang&      # 0.7

	( rb_volume_configure ; rb_audio_configure ) &  # 1.0
	rb_timezone&          # 0.4
	rb_hostname&          # 0.4
	wait
	( sleep 8 ; rb_xarcade2jstick ) &    # 0.1
	;;
  stop)
        rb_audio_save &

	;;
  restart|reload)
	;;
  *)
esac

exit $?
